<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="WasmBuildApp">
  <PropertyGroup>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <NoWarn>219</NoWarn>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <RunAnalyzers>false</RunAnalyzers>
    <WasmBuildAppDependsOn>PrepareForWasmBuildApp;$(WasmBuildAppDependsOn)</WasmBuildAppDependsOn>
    <WasmGenerateAppBundle>true</WasmGenerateAppBundle>
    <OutputType>library</OutputType>
    <WasmEmitSymbolMap>true</WasmEmitSymbolMap>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>
  <ItemGroup>
    <!-- keep this version to make sure it will pause in the expected line -->
    <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
  </ItemGroup>

  <ItemGroup>
    <WasmExtraFilesToDeploy Include="debugger-driver.html" />
    <WasmExtraFilesToDeploy Include="non-wasm-page.html" />
    <WasmExtraFilesToDeploy Include="wasm-page-without-assets.html" />
    <WasmExtraFilesToDeploy Include="other.js" />
    <WasmExtraFilesToDeploy Include="weather.json" />

    <!-- We want to bundle these assemblies, so build them first -->
    <ProjectReference Include="..\ApplyUpdateReferencedAssembly\ApplyUpdateReferencedAssembly.csproj" />
    <ProjectReference Include="..\ApplyUpdateReferencedAssembly2\ApplyUpdateReferencedAssembly2.csproj" />
    <ProjectReference Include="..\ApplyUpdateReferencedAssembly3\ApplyUpdateReferencedAssembly3.csproj" />
    <ProjectReference Include="..\ApplyUpdateReferencedAssemblyChineseCharInPathㄨ\ApplyUpdateReferencedAssemblyChineseCharInPathㄨ.csproj" />
    <ProjectReference Include="..\debugger-test-special-char-in-path-#@\debugger-test-special-char-in-path.csproj" ReferenceOutputAssembly="false" />
    <ProjectReference Include="..\debugger-test-chinese-char-in-path-ㄨ\debugger-test-chinese-char-in-path-ㄨ.csproj" ReferenceOutputAssembly="false" />
    <ProjectReference Include="..\debugger-test-with-source-link\debugger-test-with-source-link.csproj" ReferenceOutputAssembly="false" Private="true" />
    <ProjectReference Include="..\debugger-test-without-debug-symbols-to-load\debugger-test-without-debug-symbols-to-load.csproj" Private="true" />
    <ProjectReference Include="..\debugger-test-with-non-user-code-class\debugger-test-with-non-user-code-class.csproj" Private="true" />
    <ProjectReference Condition="!$([MSBuild]::IsOSPlatform('windows'))" Include="..\debugger-test-with-colon-in-source-name\debugger-test-with-colon-in-source-name.csproj" Private="true" />
    <ProjectReference Include="..\debugger-test-vb\debugger-test-vb.vbproj" Private="true" />
    <!-- loaded by *tests*, and not the test app -->
    <ProjectReference Include="..\lazy-debugger-test-embedded\lazy-debugger-test-embedded.csproj" ReferenceOutputAssembly="false" Private="true" />

    <!-- These are only loaded dynamically -->
    <_AssemblyForDynamicLoading Include="lazy-debugger-test" />
    <_AssemblyForDynamicLoading Include="lazy-debugger-test-chinese-char-in-path-ㄨ" />
    <_AssemblyForDynamicLoading Include="debugger-test-with-full-debug-type" />
    <_AssemblyForDynamicLoading Include="debugger-test-with-pdb-deleted" />
    <_AssemblyForDynamicLoading Include="debugger-test-without-debug-symbols" />
    <_AssemblyForDynamicLoading Include="library-dependency-debugger-test1" />
    <_AssemblyForDynamicLoading Include="library-dependency-debugger-test2" />
    <ProjectReference Include="@(_AssemblyForDynamicLoading -> '../%(Identity)/%(Identity).csproj')" ReferenceOutputAssembly="false" Private="true" />
  </ItemGroup>

  <Target Name="PrepareForWasmBuildApp" DependsOnTargets="Build">
    <Error Condition="!Exists('$(MicrosoftNetCoreAppRuntimePackRidDir)native')"
           Text="Cannot find %24(MicrosoftNetCoreAppRuntimePackRidDir)=$(MicrosoftNetCoreAppRuntimePackRidDir)native. Make sure to set the runtime configuration with %24(RuntimeConfiguration). Current value: $(RuntimeConfiguration)" />
    <ItemGroup>
      <!-- Remove pdb from System.Private.CoreLib to have the same scenario that we have in a Blazor/Wasm user app -->
      <FilesToDelete Include="$(MicrosoftNetCoreAppRuntimePackRidDir)native/System.Private.CoreLib.pdb"/>
      <!-- Remove pdb from all libraries in CI to improve CI performance and have the same scenario that we have in a Blazor/Wasm user app -->
      <FilesToDelete Condition="'$(ContinuousIntegrationBuild)' == 'true'" Include="$(MicrosoftNetCoreAppRuntimePackRidDir)lib/$(AspNetCoreAppCurrent)/*.pdb" Exclude="$(MicrosoftNetCoreAppRuntimePackRidDir)lib/$(AspNetCoreAppCurrent)/System.Console.pdb"/>
    </ItemGroup>
    <Delete Files="@(FilesToDelete)"/>
    <PropertyGroup>
      <EnableDefaultWasmAssembliesToBundle>false</EnableDefaultWasmAssembliesToBundle>
      <WasmAppDir>$(AppDir)</WasmAppDir>
      <!-- -1 enabled debugging and disables debug logging. -->
      <WasmDebugLevel Condition="'$(WasmDebugLevel)'==''">-1</WasmDebugLevel>
      <WasmResolveAssembliesBeforeBuild>true</WasmResolveAssembliesBeforeBuild>
    </PropertyGroup>

    <ItemGroup>
      <WasmAssembliesToBundle Include="$(OutDir)\$(TargetFileName)" />
      <WasmAssembliesToBundle Include="$(OutDir)\debugger-test-special-char-in-path.dll" />
      <WasmAssembliesToBundle Include="$(OutDir)\debugger-test-chinese-char-in-path-ㄨ.dll" />
      <WasmAssembliesToBundle Include="$(OutDir)\debugger-test-with-source-link.dll" />
      <WasmAssembliesToBundle Include="$(OutDir)\debugger-test-without-debug-symbols-to-load.dll" />
      <WasmAssembliesToBundle Include="$(OutDir)\debugger-test-with-non-user-code-class.dll" />
      <WasmAssembliesToBundle Condition="!$([MSBuild]::IsOSPlatform('windows'))" Include="$(OutDir)\debugger-test-with-colon-in-source-name.dll" />
      <WasmAssembliesToBundle Include="$(OutDir)\debugger-test-vb.dll" />
      <WasmAssembliesToBundle Include="$(OutDir)\Newtonsoft.Json.dll" />
      <WasmAssembliesToBundle Include="$(MicrosoftNetCoreAppRuntimePackRidDir)\lib\$(NetCoreappCurrent)\System.Runtime.InteropServices.JavaScript.dll" />

      <!-- Assemblies only dynamically loaded -->
      <WasmFilesToIncludeInFileSystem Include="$(OutDir)\%(_AssemblyForDynamicLoading.Identity).dll" />

      <WasmExtraFilesToDeploy Include="debugger-main.js" />
      <WasmExtraFilesToDeploy Include="$(OutDir)\%(_AssemblyForDynamicLoading.Identity).*" />
      <WasmExtraFilesToDeploy Include="$(OutDir)\lazy-debugger-test-embedded.*" />
      <WasmExtraFilesToDeploy Include="@(ReferenceCopyLocalPaths)" />

      <WasmAssemblySearchPaths Include="$(MicrosoftNetCoreAppRuntimePackRidDir)native"/>
      <WasmAssemblySearchPaths Include="$(MicrosoftNetCoreAppRuntimePackRidDir)lib\$(NetCoreAppCurrent)"/>
      <WasmAssemblySearchPaths Include="$(OutDir)"/>
    </ItemGroup>
  </Target>
  <Target Name="PreserveEnCAssembliesFromLinking"
          Condition="'$(TargetOS)' == 'browser' and '$(EnableAggressiveTrimming)' == 'true'"
          BeforeTargets="ConfigureTrimming">
    <ItemGroup>
      <!-- Don't modify EnC test assemblies -->
      <TrimmerRootAssembly
          Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('ApplyUpdateReferencedAssembly.dll'))"
          Include="%(ResolvedFileToPublish.FullPath)" />
      <TrimmerRootAssembly
          Condition="$([System.String]::Copy('%(ResolvedFileToPublish.FileName)%(ResolvedFileToPublish.Extension)').EndsWith('ApplyUpdateReferencedAssemblyChineseCharInPathㄨ.dll'))"
          Include="%(ResolvedFileToPublish.FullPath)" />
    </ItemGroup>
  </Target>
  <Target Name="IncludeDeltasInWasmBundle"
          BeforeTargets="PrepareForWasmBuildApp"
          Condition="'$(TargetOS)' == 'browser'">
    <ItemGroup>
      <!-- FIXME: this belongs in eng/testing/tests.wasm.targets -->
      <!-- FIXME: Can we do something on the Content items in the referenced projects themselves to get this for free? -->
      <WasmFilesToIncludeInFileSystem Include="@(PublishItemsOutputGroupOutputs)"
                                      Condition="$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.dmeta'))" />
      <WasmFilesToIncludeInFileSystem Include="@(PublishItemsOutputGroupOutputs)"
                                      Condition="$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.dil'))" />
      <WasmFilesToIncludeInFileSystem Include="@(PublishItemsOutputGroupOutputs)"
                                      Condition="$([System.String]::new('%(PublishItemsOutputGroupOutputs.Identity)').EndsWith('.dpdb'))" />
    </ItemGroup>
  </Target>

  <Import Project="$(BrowserProjectRoot)\build\WasmApp.InTree.targets" />
</Project>
